<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userForm" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:">
                <el-input v-model="userInfo.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情">
            <!-- 放置个人详情 -->
            <component :is="userComponent" />
            <!-- <user-info /> -->
          </el-tab-pane>

          <el-tab-pane label="岗位信息">
            <!-- 放置岗位详情 -->
            <component :is="JobInfo" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'

import { saveUserDetailById } from '@/api/employees'

// 详情-个人详情
import userComponent from './components/user-info'
// 详情-岗位信息
import JobInfo from './components/job-info'

export default {
  // 注册组件
  components: {
    userComponent,
    JobInfo
  },

  // 数据
  data() {
    return {
      // 组件名
      userComponent: 'userComponent',
      JobInfo: 'JobInfo',

      userInfo: {
        username: '',
        password2: ''
      }
    }
  },
  // 钩子
  created() {
    this.getUserInfo() // 获取员工信息
  },

  // 方法
  methods: {
    // 获取员工信息
    async getUserInfo() {
      // console.log(this.$route.params.id)
      const res = await getUserDetailById(this.$route.params.id)
      this.userInfo = { ...this.userInfo, ...res }
    },
    async saveUser() {
      if (this.userInfo.password2 === '') {
        this.$message.error('密码不能为空')
        return
      }

      try {
        await this.$refs.userForm.validate() // 校验
        await saveUserDetailById({ ...this.userInfo, password: this.userInfo.password2 })
        await this.$message.success('保存成功')
        this.$router.go(0)
      } catch (error) {
        this.$message.error(error)
      }
    }
  }
}
</script>

<style>

</style>
